package com.cpi.cornpp.manualcharges.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.MappingDispatchAction;
import org.apache.struts.util.MessageResources;

import KC.ModSegApp.UsuarioApp;

import com.cpi.cornpp.biweeks.services.BiWeekService;
import com.cpi.cornpp.common.exception.CornPPException;
import com.cpi.cornpp.costelement.services.CostElementServices;
import com.cpi.cornpp.manualcharges.exception.ManualChargesException;
import com.cpi.cornpp.manualcharges.form.ManualChargesForm;
import com.cpi.cornpp.manualcharges.services.ManualChargesService;
import com.cpi.cornpp.manualcharges.view.ManualChargesFilterSearchView;
import com.cpi.cornpp.manualcharges.view.ManualChargesView;
import com.cpi.cornpp.purchase.program.exception.PurchaseProgramException;
import com.cpi.cornpp.purchaseorder.service.PurchaseOrderVersionService;
import com.cpi.cornpp.purchaseorder.view.PurchaseOrderVersionView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.user.view.UserView;
import com.cs.common.utils.NumberUtil;
/**
 * 
 * Manual Charges Action
 * 
 * @author Edgar Joao
 * @see MappingDispatchAction
 *
 */
public class ManualChargesAction extends MappingDispatchAction {
	/** Default forward */
	private final static String DEFAULT_FORWARD = "default";//$NON-NLS-1$
	/** Custom exception */
	private final static String CUSTOM_EXCEPTION = "custom_exception";//$NON-NLS-1$
	/** Id de version */
	private final static String VERSION_ID_LABEL = "versionId";//$NON-NLS-1$
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success forward*/
	private static final String SUCCESS_FORWARD = "success";//$NON-NLS-1$
	/** Success edit key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	/** Success delete key */
	private static final String SUCCESS_DELETE_LABEL = "common.delete.success";//$NON-NLS-1$
	private static final String SUCCESS_ADD_LABEL = "common.add.success";//$NON-NLS-1$
	/** Identifier in the Request for error messages*/
	private static final String ERROR_MESSAGE = "ERROR_MESSAGE";
	/** Mensaje de error seleccion de resultados vacia*/
	private static final String SELECT_AN_ITEM = "error.message.futurepurchase.select.element.results";
	private static final String SELECT_CHANGE_TYPE = "warning.message.manualcharge.chargetype";
	/** Nombre de variable de session para el usuario */
	private static final String USER_SESSION_NAME = "user";//$NON-NLS-1$
	/**
	 * Muestra el listado de Costos Manuales
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward showListManualCharges(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		ManualChargesService service = null;
		HttpSession httpSession = request.getSession(true);
		ManualChargesForm manualChargesForm;
		int versionId = 0;
		
		try{
			service = new ManualChargesService();
			manualChargesForm = (ManualChargesForm) form;
			versionId = NumberUtil.getInt(httpSession.getAttribute(VERSION_ID_LABEL));
			manualChargesForm.setManualChargesList(service.getManualChargesList(versionId));
		}catch (ManualChargesException manualChargesException){
			httpSession.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		}catch (Exception exception){
			ManualChargesException manualChargesException = null;
			exception.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(exception, CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			httpSession.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		}
							
		return mapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Redirecciona a la pantalla de agregar
	 * 
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToAddManualCharge(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ManualChargesForm manualChargesForm = null;
		ManualChargesFilterSearchView filterSearch = null;
		TransportService transportService = null;
		List transportListViews 		  = null;	
		BiWeekService biWeekService       = null;
		List biWeekList                   = null;		
		CostElementServices costService   = null;
		Collection costElementsList       = null;
		HttpSession session 			  = null;		
		session = request.getSession(false);
		int versionId = 0;
		try{
			versionId = NumberUtil.getInt(session.getAttribute(VERSION_ID_LABEL));
			manualChargesForm = (ManualChargesForm) form;
			transportService   = new TransportService();
			costService        = new CostElementServices();
			biWeekService      = new BiWeekService();
			biWeekList         = new ArrayList(0);
			costElementsList   = new ArrayList(0);
			transportListViews = new ArrayList(0);			
			transportListViews = transportService.getTransports();			
			costElementsList   = costService.getCostElementList();
			biWeekList         = biWeekService.getBiWeeks(versionId);
			
			session.setAttribute("biWeekList", biWeekList);
			session.setAttribute("transportListViews", transportListViews);			
			session.setAttribute("costElementsList", costElementsList);
			
		} catch(TransportException transportException){
			session.setAttribute(CUSTOM_EXCEPTION, transportException);
			throw transportException;
		} catch(Exception e){
			TransportException transportException=null;
			e.printStackTrace(System.out);
			transportException= new TransportException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, transportException);
			throw transportException;
		}
		filterSearch = new ManualChargesFilterSearchView();		
		filterSearch.clearValues();
		manualChargesForm.setFilterSearch(filterSearch);
		manualChargesForm.setPurchaseOrdersVersionList(new ArrayList(0));
		manualChargesForm.setManualChargesList(new ArrayList(0));
		manualChargesForm.setSelectedPurchaseOrdersIds(new String[]{});
		manualChargesForm.setView(new ManualChargesView());
		manualChargesForm.reset(mapping, request);
		form.reset(mapping, request);
		return mapping.findForward(DEFAULT_FORWARD);
	}	
	/**
	 * Agrega un cargo manual
	 * 
	 * @author Edgar Joao
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	public ActionForward addManualCharge(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		ManualChargesService service = null;
		ManualChargesForm manualChargesForm = null;
		MessageResources messageResources = null;
		UserView userView = null;		
		UsuarioApp usuarioApp;
		String messageError = null;				
		HttpSession session  = null;		
		session = request.getSession(false);
		String[] selectedIds = null;
		List aux;
		List selectedPurchases = null;
		String versionId;
		try{
			versionId = (String) session.getAttribute("versionId");					
			messageResources = getResources(request);
			manualChargesForm = (ManualChargesForm)form;			
			
			if(manualChargesForm.getView().getChargeType() == null || StringUtils.equals(manualChargesForm.getView().getChargeType(), StringUtils.EMPTY)){
				request.getSession(false).setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SELECT_CHANGE_TYPE));
				return mapping.findForward(DEFAULT_FORWARD);	
			}
						
			selectedIds = manualChargesForm.getSelectedPurchaseOrdersIds();
			if(selectedIds!=null && !(selectedIds.length > 0)){
				request.getSession(false).setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SELECT_AN_ITEM));
				return mapping.findForward(DEFAULT_FORWARD);
			}										
			aux = Arrays.asList(selectedIds);			
			service = new ManualChargesService();
			selectedPurchases = service.purchasesSelectedIdsHelper(aux, manualChargesForm.getPurchaseOrdersVersionList());			
			manualChargesForm.getView().setVersionId(NumberUtil.parseInt(versionId));
			//Se obtiene el usuario
			userView = (UserView) session.getAttribute(USER_SESSION_NAME);
			usuarioApp =  (UsuarioApp) userView.getUserObject();
			manualChargesForm.getView().setUserId(usuarioApp.getLogin());
			//Metodo del servicio que inserta un Costo Manual
			service.insertManualCharges(manualChargesForm.getView(), selectedPurchases);
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_ADD_LABEL);
			session.setAttribute(MESSAGE, messageError);			
		} catch(ManualChargesException manualChargesException){
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		} catch(Exception e){
			ManualChargesException manualChargesException=null;
			e.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		}
		ManualChargesFilterSearchView filterSearch = new ManualChargesFilterSearchView();		
		filterSearch.clearValues();
		manualChargesForm.setFilterSearch(filterSearch);
		manualChargesForm.setPurchaseOrdersVersionList(new ArrayList(0));
		manualChargesForm.setView(new ManualChargesView());
		manualChargesForm.setSelectedPurchaseOrdersIds(new String[0]);
		manualChargesForm.setSizeList(0);
		manualChargesForm.setManualChargesList(new ArrayList(0));
		manualChargesForm.reset(mapping, request);
		form.reset(mapping, request);
		return mapping.findForward(SUCCESS_FORWARD);
	}	
	/**
	 * Metodo para buscar ordenes de compra
	 * 
	 * @author Edgar Joao
	 * 
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward filterSearchPurchaseOrdersVersions(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		PurchaseOrderVersionService service = null;		
		ManualChargesForm form  			= null;
		HttpSession session 			  	= null;
		String versionId 					= null;
		List result                         = null;		
		session = argRequest.getSession(false);
		try{			
			service = new PurchaseOrderVersionService();
			form = (ManualChargesForm) argForm;
			result = new ArrayList(0);
			
			form.setSizeList(-1);
			versionId = (String) session.getAttribute("versionId");
			result = service.getPurchasesOrdersVersionsList(form.getFilterSearch(), NumberUtil.parseInt(versionId));			
			form.setPurchaseOrdersVersionList(result);
			form.setView(new ManualChargesView());
			if(result.size() > 0){				
				form.setSizeList(result.size());
			}
		} catch (PurchaseProgramException purchaseProgramException) {
			session.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		}					
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * 
	 * Metodo que busca ordenes de compra - Editar
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward filterSearchPurchaseOrdersVersionsEdit(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {		
		PurchaseOrderVersionService service = null;		
		ManualChargesForm form  			= null;
		HttpSession session 			  	= null;		
		List result                         = null;	
		List purchaseOrdersVersionList      = null;
		String total 						= null;
		ManualChargesService mService 	    = null;
		session = argRequest.getSession(false);
		try{
			purchaseOrdersVersionList = new ArrayList(0);			
			service = new PurchaseOrderVersionService();
			mService = new ManualChargesService();
			form = (ManualChargesForm) argForm;
			result = new ArrayList(0);							
			result = service.getPurchasesOrdersVersionsList(form.getFilterSearch(), form.getView().getVersionId());
			purchaseOrdersVersionList = form.getPurchaseOrdersVersionList();		
			if(result.isEmpty()){
				purchaseOrdersVersionList = mService.getPurchaseOrderList(form.getView().getVersionId(), form.getView().getManualChargeId(), form.getView().getChargeType(), form.getView().getAmount());;
			}else{
				purchaseOrdersVersionList = this.addPurchaseVersionList(form.getPurchaseOrdersVersionList(), result);	
			}			
			total = mService.getTotal(purchaseOrdersVersionList);
			argRequest.setAttribute("total", total);
			form.setPurchaseOrdersVersionList(purchaseOrdersVersionList);
			if(purchaseOrdersVersionList.size() > 0){				
				form.setSizeList(purchaseOrdersVersionList.size());
			}
		} catch (PurchaseProgramException purchaseProgramException) {
			session.setAttribute(CUSTOM_EXCEPTION, purchaseProgramException);
			throw purchaseProgramException;
		} catch (Exception e){
			PurchaseProgramException ppe = null;
			e.printStackTrace(System.out);
			ppe = new PurchaseProgramException(e , CornPPException.LAYER_SERVICE,CornPPException.ACTION_INSERT);
			session.setAttribute(CUSTOM_EXCEPTION, ppe);
			throw ppe;
		}					
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Lista que verifica del resultado de la busqueda
	 * Ordenes de Compra repetidos
	 * 
	 * @author Edgar Joao
	 * @param auxList
	 * @param list
	 * @return
	 */
	private List addPurchaseVersionList(List actualList, List list){
		Iterator iterator = list.iterator();
		List resultList = null;
		PurchaseOrderVersionView view = null;
		resultList = new ArrayList(0);
		resultList.addAll(actualList);
		//if(!list.isEmpty()){
			while(iterator.hasNext()){
				view = (PurchaseOrderVersionView) iterator.next();
				if(!resultList.contains(view)){
					resultList.add(view);
				}	
			}
		//}else{
		//	resultList.addAll(actualList);
		//}		
		return resultList;
	}
	/**
	 * Metodo que elimina un Cargo Manual
	 * 
	 * @author Edgar Joao	
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward deleteManualCharges(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		HttpSession session = null;		
		ManualChargesService service = null;
		MessageResources messageResources = null;
		String messageError = null;	
		String manualChargeId;
		
		session = argRequest.getSession();
		try{			
			manualChargeId = argRequest.getParameter("manualChargeId");			
			service = new ManualChargesService();
			messageResources = getResources(argRequest);
			service.deleteManualCharges(NumberUtil.parseInt(manualChargeId));
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_DELETE_LABEL);
			session.setAttribute(MESSAGE, messageError);			
		} catch(ManualChargesException manualChargesException){
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		} catch(Exception e){
			ManualChargesException manualChargesException=null;
			e.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		}
		return argMapping.findForward(SUCCESS_FORWARD);
	}
	/**
	 * Metodo que Reedirecciona a la pantalla de editar
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToEditManualCharge(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		TransportService transportService          = null;
		List transportListViews 		           = null;	
		BiWeekService biWeekService                = null;
		List biWeekList                            = null;		
		CostElementServices costService            = null;
		Collection costElementsList                = null;
		HttpSession session 			           = null;
		ManualChargesService mService              = null;
		String manualChargeId                      = null;
		ManualChargesView view           		   = null;
		ManualChargesForm chargesForm     		   = null;
		ManualChargesFilterSearchView filterSearch = null;		
		List purchaseOrdersList = null;
		String total = null;
		session = argRequest.getSession(false);
		int versionId = 0;
		try{
			versionId = NumberUtil.getInt(session.getAttribute(VERSION_ID_LABEL));
			transportService   = new TransportService();
			costService        = new CostElementServices();
			biWeekService      = new BiWeekService();
			biWeekList         = new ArrayList(0);
			costElementsList   = new ArrayList(0);
			transportListViews = new ArrayList(0);			
			transportListViews = transportService.getTransports();			
			costElementsList   = costService.getCostElementList();
			biWeekList         = biWeekService.getBiWeeks(versionId);
			mService           = new ManualChargesService();
			view               = new ManualChargesView();
			chargesForm = (ManualChargesForm) argForm;
			purchaseOrdersList = new ArrayList(0);			
			
			manualChargeId = argRequest.getParameter("manualChargeId");
			view = mService.getManualChargesById(NumberUtil.parseInt(manualChargeId)); 
			chargesForm.setView(view);
			
			purchaseOrdersList = mService.getPurchaseOrderList(view.getVersionId(), view.getManualChargeId(), view.getChargeType(), view.getAmount());
			//Se obtienen los Id seleccionados			
			chargesForm.setSelectedPurchaseOrdersIds(this.fillSelectedIds(purchaseOrdersList));
			
			session.setAttribute("biWeekList", biWeekList);
			session.setAttribute("transportListViews", transportListViews);			
			session.setAttribute("costElementsList", costElementsList);
			chargesForm.setPurchaseOrdersVersionList(purchaseOrdersList);
			total = mService.getTotal(purchaseOrdersList);
			argRequest.setAttribute("total", total);
			if(purchaseOrdersList.size() > 0){				
				chargesForm.setSizeList(purchaseOrdersList.size());
			}
		} catch(TransportException transportException){
			session.setAttribute(CUSTOM_EXCEPTION, transportException);
			throw transportException;
		} catch(Exception e){
			TransportException transportException=null;
			e.printStackTrace(System.out);
			transportException= new TransportException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, transportException);
			throw transportException;
		}		
		filterSearch = new ManualChargesFilterSearchView();		
		filterSearch.clearValues();
		chargesForm.setFilterSearch(filterSearch);				
		return argMapping.findForward(DEFAULT_FORWARD);
	}
	/**
	 * Metodo que obtiene una lista de Ordenes de Compra
	 * y regresa los Ids.
	 * 
	 * @author Edgar Joao
	 * @param purchaseOrderVersionList
	 * @return
	 */
	private String[] fillSelectedIds(List purchaseOrderVersionList){
		PurchaseOrderVersionView view = null;
		Iterator iterator = null;
		String[] selected = new String[purchaseOrderVersionList.size()];
		iterator = purchaseOrderVersionList.iterator();
		int cont = 0;		
		while(iterator.hasNext()){
			view = (PurchaseOrderVersionView) iterator.next();			
			selected[cont++] = new String(view.getPurchaseOrderVersionId().toString());					
		}
		return selected;
	}
	/**
	 * Metodo que edita un Cargo Manual
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward editManualCharge(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {
		
		ManualChargesService service = null;
		ManualChargesForm manualChargesForm = null;
		MessageResources messageResources = null;
		UserView userView = null;		
		UsuarioApp usuarioApp;
		String messageError = null;				
		HttpSession session  = null;		
		session = argRequest.getSession(false);
		String[] selectedIds = null;
		List aux;
		List selectedPurchases = null;
		String versionId;
		try{
			versionId = (String) session.getAttribute("versionId");
			messageResources = getResources(argRequest);			
			manualChargesForm = (ManualChargesForm)argForm;			
			
			if(manualChargesForm.getView().getChargeType() == null || StringUtils.equals(manualChargesForm.getView().getChargeType(), StringUtils.EMPTY)){
				argRequest.getSession(false).setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SELECT_CHANGE_TYPE));
				return argMapping.findForward(DEFAULT_FORWARD);	
			}
			selectedIds = manualChargesForm.getSelectedPurchaseOrdersIds();
			if(selectedIds!=null && !(selectedIds.length > 0)){
				argRequest.getSession(false).setAttribute(ERROR_MESSAGE, messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SELECT_AN_ITEM));
				return argMapping.findForward(DEFAULT_FORWARD);
			}										
			aux = Arrays.asList(selectedIds);			
			service = new ManualChargesService();
			selectedPurchases = service.purchasesSelectedIdsHelper(aux, manualChargesForm.getPurchaseOrdersVersionList());			
			manualChargesForm.getView().setVersionId(NumberUtil.parseInt(versionId));
			//Se obtiene el usuario
			userView = (UserView) session.getAttribute(USER_SESSION_NAME);
			usuarioApp =  (UsuarioApp) userView.getUserObject();
			manualChargesForm.getView().setUserId(usuarioApp.getLogin());						
			//Metodo del servicio que actualiza un Costo Manual			
			service.updateManualCharges(manualChargesForm.getView(), selectedPurchases);
			messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
			session.setAttribute(MESSAGE, messageError);			
		} catch(ManualChargesException manualChargesException){
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		} catch(Exception e){
			ManualChargesException manualChargesException=null;
			e.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_UPDATE);
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		}
		ManualChargesFilterSearchView filterSearch = new ManualChargesFilterSearchView();		
		filterSearch.clearValues();
		manualChargesForm.setFilterSearch(filterSearch);
		manualChargesForm.setPurchaseOrdersVersionList(new ArrayList(0));
		manualChargesForm.setManualChargesList(new ArrayList(0));
		manualChargesForm.setSelectedPurchaseOrdersIds(new String[]{});
		return argMapping.findForward(SUCCESS_FORWARD);
	}
	/**
	 * Metodo que muestra el detalle de el Costo Manual
	 * 
	 * @author Edgar Joao
	 * @param argMapping
	 * @param argForm
	 * @param argRequest
	 * @param argResponse
	 * @return
	 * @throws Exception
	 */
	public ActionForward goToManualChargeDetail(ActionMapping argMapping, ActionForm argForm,
			HttpServletRequest argRequest, HttpServletResponse argResponse)
			throws Exception {		
		HttpSession session = null;
		ManualChargesForm manualChargesForm = null;
		ManualChargesView view = null;
		ManualChargesService service = null;
		List purchaseOrdersList = null;		
		String manualChargeId;		
		session = argRequest.getSession();
		String total = null;	
		try{
			manualChargesForm = (ManualChargesForm) argForm;
			view = new ManualChargesView();
			purchaseOrdersList = new ArrayList(0);
			service = new ManualChargesService();			
			
			manualChargeId = argRequest.getParameter("manualChargeId");
			view = service.getManualChargesById(NumberUtil.parseInt(manualChargeId));			
			purchaseOrdersList = service.getPurchaseOrderList(view.getVersionId(), view.getManualChargeId(), view.getChargeType(), view.getAmount());
			total = service.getTotal(purchaseOrdersList);
			manualChargesForm.setView(view);
			manualChargesForm.setPurchaseOrdersVersionList(purchaseOrdersList);
			argRequest.setAttribute("total", total);
		} catch(ManualChargesException manualChargesException){
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		} catch(Exception e){
			ManualChargesException manualChargesException=null;
			e.printStackTrace(System.out);
			manualChargesException= new ManualChargesException(e , CornPPException.LAYER_ACTION,CornPPException.ACTION_LISTS);
			session.setAttribute(CUSTOM_EXCEPTION, manualChargesException);
			throw manualChargesException;
		}	
		return argMapping.findForward(DEFAULT_FORWARD);
	}
}